package mz.behaveas.model.spec {
	import mz.behaveas.model.expectation.ExpectationFactory;

	/**
	 * Creates an Specification implementation from an anonymous object instance.
	 * //TODO make the keywords localizable
	 * 
	 * @author maxim.zaks@gmail.com
	 */
	public class SpecificationFactory {
		
		private static var DESC_KEYWORD : String = "description";
		private static var EXPECTATIONS_KEYWORD : String = "expectations";

		public static function createSpec(input : *) : Specification {
			if(input[DESC_KEYWORD] == undefined || !(input[DESC_KEYWORD] is String)) {
				return new MallformedSpecification(MallformedSpecificationErrorText.DESCRIPTION);
			}
			
			var description : String = input[DESC_KEYWORD];
			
			if(input[EXPECTATIONS_KEYWORD] == undefined || !(input[EXPECTATIONS_KEYWORD] is Array)) {
				return new MallformedSpecification(MallformedSpecificationErrorText.EXPECTATIONS);
			}
			var expectations : Array = input[EXPECTATIONS_KEYWORD];
			
			var spec : Specification = new CorrectSpecification(description);
			
			for each (var expectation : * in expectations) {
				spec.addExpectation(ExpectationFactory.createExpectation(expectation));
			}
			
			return spec;
		}
	}
}
